﻿using Azure;
using Flurl.Http;
using HandyControl.Controls;
using Newtonsoft.Json;
using NewWms.Extend;
using wms_wpf.InPut;

namespace wms_wpf.Apiservice
{
    public class EssService : IEssService
    {

        public async Task<string> CreateTask(Ess_Input input)
        {
            try
            {
                if (GlobalVariable.IsDebug == "true")
                {
                    return "OK";
                }
                var url = $"http://192.168.2.10:9046/task/create";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                    if (!res.Contains("OK"))
                    {
                        Growl.Info(input.tasks.Select(x => x.taskCode).First().ToString() + "下发失败", "msg");
                        LognetHelper.WriteLog("下发失败" + input.taskType + input.taskGroupCode + input.groupPriority + input.tasks + "状态:" + Response.msg);
                    }
                    else
                    {
                        Growl.Success("下发成功", "msg");
                        LognetHelper.WriteLog("下发成功" + input.taskType + input.taskGroupCode + input.groupPriority + input.tasks + "状态:" + Response.msg);

                    }

                    return res;
                }
                else
                {
                    Growl.Info("任务下发失败", "msg");
                    return Response.msg;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("任务下发失败", "msg");
                LognetHelper.WriteLog("ESS任务下发错误:CreateTask", ex);
                return null;
            }
        }//下发任务
        public async Task<string> moveContainer(string BoxCode)//容器流动
        {
            try
            {
                Ess_Input2 input = new Ess_Input2();
                input.direction = "100";
                if (GlobalVariable.Machine == "2")
                {
                    input.slotCode = "2-4";
                }
                else
                {

                    input.slotCode = "1-4";
                }
                input.containerCode = BoxCode;
                var url = $"http://192.168.2.10:9046/conveyor/moveContainer";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                    return res;
                }
                else
                {
                    Growl.Info("容器流动失败", "msg");
                    return Response.msg;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("容器流动失败", "msg");
                LognetHelper.WriteLog("容器流动错误:moveContainer", ex);
                return null;
            }
        }
        public async Task<string> queryNodeReader()//拣选位查询
        {
            try
            {
                Ess_Input3 input = new Ess_Input3();
                if (GlobalVariable.Machine == "2")
                {
                    input.slotCode = "2-4";
                }
                else
                {
                    input.slotCode = "1-4";
                }
                var url = $"http://192.168.2.10:9046/expand/api/conveyor/queryNodeReader";
                var data = input;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss2 Response = JsonConvert.DeserializeObject<ApiResponseEss2>(result);
                string res = Response.data.barcode;
                if (Response.msg == "success")
                {
                    return res;

                }
                else
                {
                    Growl.Info("请求错误", "msg");
                    return Response.msg;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("queryNodeReader接口调用失败", "msg");
                LognetHelper.WriteLog("queryNodeReader", ex);
                return null;
            }

        }
        public async Task<string> sendCommand(Ess_Input4 inPut)//电子标签亮灯
        {
            try
            {
                if (GlobalVariable.IsDebug == "true")
                {
                    return "OK";
                }
                var url = "http://10.54.155.185:9046/expand/api/equipment/ptl/sendCommand";
                var data = inPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponseEss Response = JsonConvert.DeserializeObject<ApiResponseEss>(result);
                string res = Response.data.ToString();
                if (Response.msg == "success")
                {
                    if (!res.Contains("OK"))
                    {
                        Growl.Info("亮灯失败", "msg");
                        LognetHelper.WriteLog("亮灯失败" + inPut.controllerCode + "-" + inPut.index + "-" + inPut.tagCode + "-" +
                        inPut.color + "-" + inPut.color + "-" + inPut.mode + inPut.rank + "-" + inPut.display + "-" + "状态:" + Response.msg);
                    }
                    else
                    {
                        LognetHelper.WriteLog("亮灯成功" + inPut.controllerCode + "-" + inPut.index + "-" + inPut.tagCode + "-" +
                        inPut.color + "-" + inPut.color + "-" + inPut.mode + inPut.rank + "-" + inPut.display + "-" + "状态:" + Response.msg);

                    }

                    return res;
                }
                else
                {
                    Growl.Info("任务下发失败", "msg");
                    return Response.msg;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("亮灯失败", "msg");
                LognetHelper.WriteLog("亮灯失败" + inPut.controllerCode + "-" + inPut.index + "-" + inPut.tagCode + "-" +
                inPut.color + "-" + inPut.color + "-" + inPut.mode + inPut.rank + "-" + inPut.display + "-" + "状态:");
                return null;
            }
        }
    }
}
